package com.ease.platform.access.namedstatement.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ease.document.freemarker.FreeMarkerUtil;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class NamedStatementGeneratingUtil {

	private static final transient Log log = LogFactory.getLog(NamedStatementGeneratingUtil.class);

	@Deprecated
	public static String generate(String templatePath, Object parameter, boolean removeLineFeedCode) {
		String namedStatement = FreeMarkerUtil.processFile(templatePath, parameter);
		if (null != namedStatement && true == removeLineFeedCode) {
			namedStatement = removeWrap(namedStatement);
		}
		log.debug("NamedStatement: " + namedStatement);
		return namedStatement;

	}

	@Deprecated
	public static String generate(String templatePath, Object parameter) {
		return generate(templatePath, parameter, false);
	}

	public static String generate(Class<?> classPackageAsResourcePath, String fileName, Object parameter) {
		return generate(classPackageAsResourcePath, fileName, parameter, false);
	}

	public static String generate(Class<?> classPackageAsResourcePath, String fileName, Object parameter, boolean removeLineFeedCode) {
		String namedStatement = FreeMarkerUtil.processFile(classPackageAsResourcePath, fileName, parameter);
		if (null != namedStatement && true == removeLineFeedCode) {
			namedStatement = removeWrap(namedStatement);
		}
		log.debug("NamedStatement: " + namedStatement);
		return namedStatement;
	}

	public static String removeWrap(String string) {
		if (null != string) {
			return StringUtils.replaceChars(string, "\t\r\n\b", " ");
			// return CharSetUtils.delete(string, );
		}
		return null;
	}

}
